#define _CRT_SECURE_NO_WARNINGS

#include "string.h"

namespace bit
{
	string::string(const char* str)
		:_size(strlen(str))
	{
		_capacity = _size;
		_str = new char[_capacity + 1];
		strcpy(_str, str);
	}

	/*string::string(const string& str)
	{
		_str = new char[str._capacity + 1];
		strcpy(_str, str._str);
		_size = str._size;
		_capacity = str._capacity;
	}*/

	string::string(const string& str)
		:_str(nullptr),
		_size(0),
		_capacity(0)
	{
		string tmp(str._str);

		swap(tmp);
	}

	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_size = _capacity = 0;
	}

	const char* string::c_str() const
	{
		return _str;
	}

	size_t string::size() const
	{
		return _size;
	}

	size_t string::capacity() const
	{
		return _capacity;
	}

	char& string::operator[](size_t pos)
	{
		assert(pos < _size);

		return _str[pos];
	}

	const char& string::operator[](size_t pos) const
	{
		assert(pos < _size);

		return _str[pos];
	}

	string::iterator string::begin()
	{
		return _str;
	}
	string::const_iterator string::begin() const
	{
		return _str;
	}

	string::iterator string::end()
	{
		return _str + _size;
	}
	string::const_iterator string::end() const
	{
		return _str + _size;
	}

	void string::push_back(char ch)
	{
		if (_size == _capacity)
		{
			size_t newcpacity = _capacity == 0 ? 4 : 2 * _capacity;

			reverse(newcpacity);
		}

		_str[_size] = ch;
		_str[_size + 1] = '\0';
		++_size;
	}
	void string::append(const char* str)
	{
		size_t len = strlen(str);

		if (len + _size > _capacity)
		{
			reverse(_size + len);
		}
		
		strcpy(_str + _size, str);
		_size = _size + len;
	}

	void string::reverse(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;

			_str = tmp;

			_capacity = n;
		}
	}

	string& string::operator+=(char ch)
	{
		push_back(ch);

		return *this;
	}
	string& string::operator+=(const char* str)
	{
		append(str);

		return *this;
	}

	void string::insert(size_t pos, char ch)
	{
		assert(pos <= _size);

		if (_size == _capacity)
		{
			size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
			reverse(newcapacity);
		}

		/*size_t end = _size + 1;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
			end--;
		}*/

		for (size_t i = _size + 1; i > pos; i--)
		{
			_str[i] = _str[i - 1];
		}

		_str[pos] = ch;
		++_size;
	}

	void string::insert(size_t pos, const char* str)
	{
		assert(pos <= _size);

		size_t len = strlen(str);

		if (len + _size > _capacity)
		{
			reverse(len + _size);
		}

		/*for (size_t i = _size + 1; i > pos; i--)
		{
			_str[i + len - 1] = _str[i - 1];
		}*/

		for (size_t i = _size + len; i > pos + len - 1; i--)
		{
			_str[i] = _str[i - len];
		}

		strncpy(_str + pos, str, len);
		_size += len;
	}
	void string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		
		if (len == npos || len + pos >= _size)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			strcpy(_str + pos, _str + pos + len);

			_size -= len;
		}

	}

	size_t string::find(char ch, size_t pos)
	{
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
			{
				return i;
			}
		}

		return npos;
	}
	size_t string::find(const char* str, size_t pos)
	{
		char* st = strstr(_str + pos, str);

		if (st != nullptr)
		{
			return st - _str;
		}
		else
		{
			return npos;
		}
	}

	/*string& string::operator=(const string& s)
	{
		if (this != &s)
		{
			char* tmp = new char[s._capacity + 1];
			strcpy(tmp, s._str);
			delete[] _str;

			_str = tmp;
			_size = s._size;
			_capacity = s._capacity;
		}

		return *this;
	}*/

	string& string::operator=(string s)
	{
		swap(s);

		return *this;
	}

	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}

	string string::substr(size_t pos, size_t len)
	{
		if (len == npos || pos + len > _size)
		{
			string s(_str + pos);

			return s;
		}
		else
		{
			string s;
			reverse(len);

			for (size_t i = pos; i < pos + len; i++)
			{
				s += _str[i];
			}

			return s;
		}
	}

	bool string::operator<(const string& s) const
	{
		return strcmp(_str, s._str) < 0;
	}
	bool string::operator<=(const string& s) const
	{
		return *this < s || *this == s;
	}
	bool string::operator==(const string& s) const
	{
		return strcmp(_str, s._str) == 0;
	}
	bool string::operator>(const string& s) const
	{
		return !(*this <= s);
	}
	bool string::operator>=(const string& s) const
	{
		return !(*this < s);
	}
	bool string::operator!=(const string& s) const
	{
		return !(*this == s);
	}

	std::ostream& operator<<(std::ostream& out, const string& s)
	{
		for (size_t i = 0; i < s.size(); i++)
		{
			out << s[i];
		}
		//std::cout << std::endl;

		return out;
	}
	std::istream& operator>>(std::istream& in, string& s)
	{
		s.clear();

		char buff[128] = { 0 };
		int i = 0;

		char ch;
		ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == 127)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}

			ch = in.get();
		}

		if (i != 0)
		{
			buff[i] = '\0';
			s += buff;
		}

		return in;
	}

	void string::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}

	const size_t string::npos = -1;
}